/********************************************************************************
*	Programmer:		Brandon Shouse
*	Class:			CS362-400
*	Project:		Assignment 4 - randomtestcard.c
*
*	Description: 	Random tester that generates 2000 random intstances to test the council room
*					card effect.
*
**********************************************************************************/
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "rngs.h"
#include "dominion.h" 
#include "dominion_helpers.h"

int checkCouncilRoom(struct gameState *post, int handPos); 

int main(int argc, char **argv)
{
    int i, n, handPos, p;
    int j, k;  
    struct gameState state;
	int temp1;
    printf("Random testing for Council Room...\n");
    SelectStream(2);
    PutSeed(3);
	
    for (j = 0; j < 2000; j++) 
	{
		temp1 = j + 1; 
		printf("Test iteration: %d\n", temp1);
        for (i = 0; i < sizeof(struct gameState); i++) 
		{
            ((char*) &state)[i] = floor(Random() * 256);
        }
 
        state.numPlayers = 2 + floor(Random() * (MAX_PLAYERS - 2));
 
    
        for (i = 0; i <= state.numPlayers; i++) 
		{
            state.deckCount[i] = floor(Random() * MAX_DECK);
            state.discardCount[i] = floor(Random() * MAX_DECK);
 
            state.handCount[i] = floor(Random() * MAX_HAND);
 
            for (k = 0; k < MAX_HAND; k++) 
			{
                state.hand[i][k] = floor(Random() * 26);
                state.discard[i][k] = floor(Random() * 26);
                state.deck[i][k] = floor(Random() * 26);
 
            }
 
        }
 
        p = floor(Random() * state.numPlayers);
        handPos = floor(Random() * state.handCount[p]);
        state.playedCardCount = floor(Random() * MAX_DECK);
        state.whoseTurn = p; 
        if ((checkCouncilRoom(&state, handPos)) == 0);
			printf("All Council Room Tests Passed.\n");
    }
 
    return 0; 
}
 
int checkCouncilRoom(struct gameState *state, int handPos){
 
    int coinBonus = 0;
    int preNumAct = state->numActions;
    struct gameState preState;
    int result;
    int currentPlayer = whoseTurn(state);
    int i;
    SelectStream(3);
    PutSeed(3);
	memcpy(&preState, state, sizeof(struct gameState));
    result = cardEffect(council_room, -1, -1, -1, state, handPos, &coinBonus);
    
	SelectStream(4);
    PutSeed(3);
    for(i = 0; i < 4; i++){
        drawCard(currentPlayer, &preState);
    }
    preState.numBuys++;
 
    for(i = 0; i<preState.numPlayers; i++)
	{
        if(i != currentPlayer)
		{
            drawCard(i, &preState);
        }
    }
    discardCard(handPos, currentPlayer, &preState, 0); 
    assert(result == 0);
    assert(preState.numBuys == state->numBuys);
 
    for(i = 1; i < preState.numPlayers; i++)
	{
        assert(preState.deckCount[i] == state->deckCount[i]);
        assert(preState.discardCount[i] == state->discardCount[i]);
        assert(preState.handCount[i] == state->handCount[i]);
 
    }
    assert(memcmp(&preState, state, sizeof(struct gameState)) == 0);
	
	return 0;
}
 

